package com.example.cart.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.cart.entity.Cart;

import java.util.Collection;
import java.util.List;

@Dao
public interface CartDao {

    @Insert
    void insert(Cart item);

    @Insert
    void batchInsert(Collection<Cart> carts);

    @Query("delete from cart")
    void deleteAll();

    @Query("select id, product_id, quantity, amount from cart limit :limit offset :offset")
    List<Cart> query(int limit, int offset);

    @Query("select id, product_id, quantity, amount from cart where product_id = :productId limit 1")
    Cart selectByProductId(String productId);

    /**
     * 查询购物车商品总数
     * @return 购物车商品总数
     */
    @Query("select sum(quantity) from cart")
    int queryTotalQuantity();

    @Update
    int update(Cart cart);
}
